import 'package:flutter/material.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

import 'package:drm/base/base_list_page.dart';
import 'package:drm/util/SpUtils.dart';
import 'package:drm/base/user.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'azd_bean.dart';
import 'package:drm/http/http_url.dart';
import 'azd_bloc.dart';

import 'azd_info_page.dart';

class AzdPage extends StatefulWidget {
  int index;
  String title;

  AzdPage(this.index, {Key key, this.title}) : super(key: key);

  @override
  _AzdPageState createState() => _AzdPageState();
}



//with AutomaticKeepAliveClientMixin
class _AzdPageState extends BaseListPageState<AzdBloc, AzdBean, AzdPage>  {
  String ztInfo = '已派工';


  int selectedIndex = 0;

//  @override
//  bool get wantKeepAlive => true;
  @override
  iniBloc() {
    return AzdBloc();
  }

  @override
  iniRefushEvent() {
    Map<String, String> params = Map();
    params.putIfAbsent('Type', () => 'AZD');
    params.putIfAbsent('Rows', () => '${AppUrl.rows}');
    params.putIfAbsent('Page', () => '$page');
    params.putIfAbsent('FWSId', () => user.fwsId);
    params.putIfAbsent('ZTInfo', () => ztInfo);
    return AzdLoadDateEvent(params, portUrl: AppUrl.getGdfk());
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return super.build(context);
  }

  @override
  Widget iniHeaderView(BuildContext context) {
    return Container(
      color: Colors.white,
      height: 50,
      child: Row(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          MaterialButton(
            color: selectedIndex == 0 ? ThemeData
                .light()
                .primaryColor : null,
            onPressed: () {
              print('0');
              selectedIndex = 0;
              ztInfo = '已派工';
              refresh();
            },
            child: Text('已派工', style: TextStyle(
                color: selectedIndex == 0 ? Colors.white : Colors.black87),),
          ),
          MaterialButton(
            color: selectedIndex == 1 ? ThemeData
                .light()
                .primaryColor : null,
            onPressed: () {
              print('1');
              selectedIndex = 1;
              ztInfo = '已安装';
              refresh();
            },
            child: Text('已安装', style: TextStyle(
                color: selectedIndex == 1 ? Colors.white : Colors.black87),),
          ),
        ],
      ),
    );
  }

  @override
  List<Widget> iniListItems(List<AzdBean> datas) {

    print(datas.length);

    return datas.map((item) {

      return itemView(item);
    }).toList();
  }

  @override
  Widget inifootView(BuildContext context) {}


  Widget itemView(AzdBean item) {

    return Card(
      elevation: 0,
        child: Padding(
          padding: EdgeInsets.only(left: 16, right: 16, bottom: 10),
          child: ListTile(
            title: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[
                Padding(padding: EdgeInsets.only(top: 5, bottom: 5),
                  child: Text('${item.kH}  ( ${item.xDSD} )', style: TextStyle(
                    fontSize: 17,),),),
                Padding(padding: EdgeInsets.only(top: 3,),
                  child: Text('电话：${item.lXDH}', style: TextStyle(
                    fontSize: 15,),),),
                Padding(padding: EdgeInsets.only(top: 3,),
                  child: Text('地址：${item.sHDZ}', style: TextStyle(
                    fontSize: 15,),),),
                Padding(padding: EdgeInsets.only(top: 3,bottom: 3),
                  child: Text('安装：${item.yYAZ}', style: TextStyle(
                    fontSize: 15,),),),
              ],
            ),
            subtitle: item.bZ.isEmpty? null : Text('${item.bZ}',maxLines: 2,overflow: TextOverflow.ellipsis,),
            onTap: () {

              if (item.cksl == '0') {
                //提示，web上的提示信息；
                toast('该单还未销售出库!');
              }else {
                bool enable = false;
                if(item == null) {
                  enable =  false;
                }
                if (item.zT == '已安装') {
                  enable =  false;
                }else {
                  enable =  true;
                }
                String title = '操作-${item.lx}';

                Navigator.of(context).push(MaterialPageRoute(builder: (context) {
                  return AzdInfoPage(title: title,azdBean: item,enable: enable,);
                }));
              }
            },
          ))

    );
  }
}
